package com.example.mywanandroid.project;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.mywanandroid.R;

import java.util.List;

public class ProjectRecyclerAdapter extends RecyclerView.Adapter<ProjectRecyclerAdapter.ProjectViewHolder> {
    private Activity activity;
    private Context context;
    private List<ProjectRecyclerViewItem> list;
    private View inflater;
    //构造方法，传入数据
    public ProjectRecyclerAdapter(Activity activity, Context context, List<ProjectRecyclerViewItem> list) {
        this.activity = activity;
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ProjectViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //创建ViewHolder，返回每一项的布局
        inflater = LayoutInflater.from(context).inflate(R.layout.project_recyclerview_item, parent, false);
        ProjectViewHolder projectViewHolder = new ProjectViewHolder(inflater);
        return projectViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ProjectViewHolder holder, final int position) {
        // 解决最后一行显示不全
        if(position == list.size()) {
            holder.itemView.setVisibility(View.GONE);
            return;
        }
        Glide.with(activity).load(list.get(position).getEnvelopePic()).into(holder.imageView);
        holder.titleTextView.setText(list.get(position).getTitle());
        holder.descTextView.setText(list.get(position).getDesc());
        holder.dateTextView.setText(list.get(position).getNiceShareDate());
        holder.authorTextView.setText(list.get(position).getAuthor());
        // 实现点击效果
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    //内部类，绑定控件
    class ProjectViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView titleTextView;
        TextView descTextView;
        TextView dateTextView;
        TextView authorTextView;
        public ProjectViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.project_recyclerview_image);
            titleTextView = itemView.findViewById(R.id.project_recyclerview_title);
            descTextView = itemView.findViewById(R.id.project_recyclerview_desc);
            dateTextView = itemView.findViewById(R.id.project_recyclerview_date);
            authorTextView = itemView.findViewById(R.id.project_recyclerview_author);
        }
    }

    // 私有属性
    private OnItemClickListener onItemClickListener = null;

    // setter方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    // 回调接口
    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    //下面两个方法提供给页面刷新和加载时调用
    public void add(List<ProjectRecyclerViewItem> addMessageList) {
        //增加数据
        int position = list.size();
        list.addAll(position, addMessageList);
        notifyItemInserted(position);
    }

    public void refresh(List<ProjectRecyclerViewItem> newList) {
        //刷新数据
        list.removeAll(list);
        list.addAll(newList);
        notifyDataSetChanged();
    }
}
